<?php
namespace Home\Controller;
use Common\Controller\CommonController;

class ArticleController extends CommonController {

	/* *
	 * Dragon
	 * 2016-11-24
	 * 文章首页
	 * */
	function index() {
		$article=M('Article');

		$keyword=I('key');
		if(!empty($keyword)){
			$where['title'] = array('like',"%".$keyword."%");
		}
		$where['cid'] = array("gt",0);
		$Count=$article->where($where)->count();
		$Page = new \Think\Pager($Count,10);
		$show=$Page->showhtml(); 

		$res = M('Article')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('ctime desc') -> select();
		$list = array();
		foreach ($res as $key => $value) {
			$category = M('Category') -> where("id='" . $value['cid'] . "'") -> find();
			$value['category'] = $category['category'];
			$list[] = $value;
		}

		$this->assign('page',$show);
		$this -> assign('list', $list);
		$this -> display();
	}

	/* *
	 * Dragon
	 * 2016-11-24
	 * 添加页
	 * */
	function article_page_add() {
		$category = M('Category') -> where('cid=3')->order('pid desc')->select();
		$advertising = M('Article_advertising')->select();

		$this -> assign('advertising', $advertising);
		$this -> assign('category', $category);
		$this -> display('add_article');
	}
	
	
	/* *
	 * Dragon
	 * 2016-11-24
	 * 单篇文章
	 * */
	function up_show() {
		$where['id']=I('id');
		$res = M('Article') ->where($where)-> find();
		$this -> assign('res', $res);
		$this -> display();
	}
	
	/* *
	 * Dragon
	 * 2016-11-24
	 * 修改单篇文章
	 * */
	function up_upshow() {
		$content=I('content');
		$where['id']=I('id');
		$data=array('content'=>html_entity_decode($content));
		$res = M('Article') ->where($where)-> save($data);
		if ($res) {
			$this -> ajaxReturn(array('s' => true, 'm' => '修改成功'));
		} else {
			$this -> ajaxReturn(array('s' => false, 'm' => '修改失败'));
		}
	}
	

	/* *
	 * Dragon
	 * 2016-11-24
	 * 修改页
	 * */
	function article_page_up() {
		$where['id'] = I('id');
		$res = M('article') -> where($where) -> find();
		$res['img'] = json_decode($res['img'], true);
		$category = M('Category') -> where('cid=3')->order('pid desc')-> select();
		$advertising = M('Article_advertising') ->  select();
		$this -> assign('advertising', $advertising);
		$this -> assign('category', $category);
		$this -> assign('res', $res);
		$this -> display('up_article');
	}

	/* *
	 * Dragon
	 * 2016-11-24
	 * 添加文章
	 * */
	function add_article() {
		$title = I('title');
		$aid = I('aid');
		$category = I('category');
		$share_content = I('share_content');
		$share_img = I('share_img');
		$list_src = json_encode(I('list_src'));
		$content = html_entity_decode(I('content'));
		$is_top = I('is_top/d', 0);
		$systems = I('systems');

		$data = array(
				'title' 	=> $title,
				'cid' 		=> $category,
				'img' 		=> $list_src,
				'content' 	=> $content,
				'aid'		=> $aid,
				'ctime' 	=> time(),
				'is_top'	=> $is_top,
				'share_content'=>$share_content,
				'share_img'=>$share_img,
				'systems'	=> $systems,
			);
		$res = M('article') -> add($data);
		if ($res) {
			$this -> ajaxReturn(array('s' => true, 'm' => '添加成功'));
		} else {
			$this -> ajaxReturn(array('s' => false, 'm' => '添加失败'));
		}

	}

	/* *
	 * Dragon
	 * 2016-11-24
	 * 修改文章
	 * */
	function up_article() {
		$where['id'] = I('id');
		
		$title = I('title');
		$aid = I('aid');
		$category = I('category');
		$share_content = I('share_content');
		$share_img = I('share_img');
		$systems = I('systems');
		$list_src = json_encode(I('list_src'));
		$content = html_entity_decode(I('content'));
		$data = array('title' => $title, 'cid' =>$category, 'img' => $list_src, 'content' => $content, 'aid'=>$aid, 'share_content'=>$share_content, 'share_img'=>$share_img, 'systems'=>$systems);

		$res = M('article') -> where($where) -> save($data);
		//echo M()->_sql();exit;
		if ($res) {
			$this -> ajaxReturn(array('s' => true, 'm' => '修改成功'));
		} else {
			$this -> ajaxReturn(array('s' => false, 'm' => '修改失败'));
		}

	}

	/* *
	 * Dragon
	 * 2016-11-24
	 * 删除文章
	 * */
	function del_article() {
		$where['id'] = I("id");
		$res = M('Article') -> where($where) -> delete();
		if ($res) {
			$this -> redirect('Article/index');
		}

	}

	/* *
	 * Dragon
	 * 2016-11-25
	 * 文章底部广告首页
	 * */
	function advertising() {
		
		$art=M('Article_advertising');
		$Count=$art->count();
		$Page = new \Think\Pager($Count,10);

		$show=$Page->showhtml();

		$res = M('Article_advertising') -> select();
		$list = array();
		foreach ($res as $key => $value) {
			// $value['img'] = json_decode($value['img'], true);
			// $value['img'] = $value['img'][0];
			$list[] = $value;
		}
		$this->assign('page',$show);
		$this->assign('list', $list);
		$this->display();

	}

	/**
	 * 广告编辑
	 */
	public function advertising_edit()
	{
        $id = I('id');
        if(IS_POST){
            $data = I('post.');
            if(empty($data['img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传图片'));
            }
            $model = M('Article_advertising');
            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('advertising')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $info = M('Article_advertising')->where(['id'=>$id])->find();
            $this->assign('info', $info);
            $this->display();
        }
	}


	/* *
	 * Dragon
	 * 2016-11-25
	 * 添加广告
	 * */
	function add_advertising() {
		$title = I('title');
		$url = I("src");
		// $img = json_encode(I("iocn"));
		$img = I("iocn");
		$desc = I('desc');
		$data = array('title' => $title, 'desc'=>$desc, 'url' => $url, 'img' => $img, );
		$res = M('Article_advertising') -> add($data);
		if ($res) {
			$this->ajaxReturn(array('s'=>true, 'm'=>'添加成功'));
		} else {
			$this->ajaxReturn(array('s'=>false, 'm'=>'添加失败'));
		}
	}
	
	/* *
	 * Dragon
	 * 2016-11-25
	 * 删除广告
	 * */
	function del_advertising() {
		$where['id'] = I("id");
		$res = M('Article_advertising') -> where($where) -> delete();
		if ($res) {
			$this -> redirect('Article/advertising');
		}
	}
}
